//
//  MCNewFeatureViewController.swift
//  HXWeibo
//
//  Created by HX on 15/11/26.
//  Copyright © 2015年 HX. All rights reserved.
//

import UIKit

class MCNewFeatureViewController: UICollectionViewController
{
    private let reuseIdentifier = "ReuseIdentifier"
    
    // MARK: - 流水布局属性
    private var flowLayout = UICollectionViewFlowLayout()
    
    private let imageCount = 4
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 实现空参数的构造函数
    init()
    {
        // 调用需要流水布局的构造函数，自己来提供流水布局
        super.init(collectionViewLayout: flowLayout)
        
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        prepareCollectionView()
    }
    
    func prepareCollectionView()
    {
        // collectionView 注册 cell
        collectionView?.registerClass(MCNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置布局
        flowLayout.itemSize = view.bounds.size
        
        //设置滚动方向(水平)
        flowLayout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        //设置图片之间的间距为0
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        // 分页
        collectionView?.pagingEnabled = true
        
        //取消弹簧效果
        collectionView?.bounces = false
        
    }
    
    // MARK: - collectionView数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        // 获取cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MCNewFeatureCell
        
        cell.index = indexPath.item
        
        return cell
    }
    
    // 当新特性的图片到最后一张，再执行按钮动画
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
    {
        // 获取正在显示的cell的indexPath
        let visibleIndexPath = collectionView.indexPathsForVisibleItems().last!
        
        // 在collectionView停下来，我们自己来判断是否是最后一个cell
        if visibleIndexPath.item == imageCount - 1
        {
            // 根据indexpath获取cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath) as! MCNewFeatureCell
            
            //按钮动画
            cell.startBtnAnimation()
        }
    }
}

// 自定义cell,显示图片
class MCNewFeatureCell: UICollectionViewCell
{
    /// 第几个cell
    var index: Int = 0
        {
        didSet
        {
            //根据是第几个cell,设置对应的图片
            bjView.image = UIImage(named: "new_feature_\(index + 1)")
            
            startBtn.hidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder)
    {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        prepareUI()
    }
    
    // 按钮动画
    func startBtnAnimation()
    {
        // 显示按钮 
        startBtn.hidden = false
        
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("按钮动画完成");
        }
    }
    
    // MARK: - 懒加载展示新特性的图片
    private lazy var bjView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    /// 开始按钮
    private lazy var startBtn: UIButton =
    {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setTitle("开始体验", forState: UIControlState.Normal)
        
        // 给按钮添加点击事件
        btn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    }()
    
    // MARK: - "开始体验"按钮的点击事件
    func startBtnClick()
    {
        // 切换控制器
        AppDelegate.switchRootViewController(true) 
    }
    
    private func prepareUI()
    {
        // 添加子控件
        contentView.addSubview(bjView)
        contentView.addSubview(startBtn)
        
        // 使用第三方 填充父控件
        bjView.ff_Fill(contentView)
        
        // 开始按钮
        startBtn.ff_AlignInner(type: ff_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -160))
    }
}






























